import { pageFixture } from './../hooks/pageFixture';
import { Given, When, Then } from '@cucumber/cucumber';
import {expect} from '@playwright/test';
import { TIMEOUT } from 'dns';

Given('the user is on the login page', {timeout:20000}, async function () {
  await pageFixture.page.goto('https://petstore.octoperf.com/actions/Account.action?signonForm=');
  await pageFixture.page.waitForSelector("input[name='username']")
});


When('the user enters valid credentials', async function () {
  await pageFixture.page.locator("input[name='username']").fill("admin");
  await pageFixture.page.locator("input[name='password']").fill("admin");
});

When('clicks on the login button', async function () {
  await pageFixture.page.locator("(//input[@type='submit'])[2]").click();
});


Then('the user should be logged in', async function () {
  let content = await pageFixture.page.locator("id=WelcomeContent").textContent();
  expect(content).toContain('Welcome admin!');
});

When('the user enters invalid credentials', async function () {
  await pageFixture.page.locator("input[name='username']").fill("admin");
  await pageFixture.page.locator("input[name='password']").fill("admin123");
});

Then('the user should see an error message', async function () {
  let errMsg = await pageFixture.page.locator("//li[normalize-space(text())='Invalid username or password. Signon failed.']").textContent();
  expect(errMsg).toContain('Invalid username or password.  Signon failed.');
});